package com.hyx.bookManager.controller;

import com.hyx.bookManager.domain.common.CommonResult;
import com.hyx.bookManager.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @ClassName AdminBookController
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/6/10 9:57
 * @Version 1.0
 **/

@CrossOrigin
@RestController
@RequestMapping("adminBook")
public class AdminBookController {

    @Autowired
    BookService bookService;

    @ResponseBody
    @RequestMapping("insertBook")
    public CommonResult insertBook(MultipartFile[] file, @Param("tag") Long tag, @Param("name") String name, @Param("desc") String desc, @Param("num") Integer num) {
        return bookService.insertBook(tag, file, name, desc, num);
    }

    @ResponseBody
    @RequestMapping("/getAllBook")
    public CommonResult getAllBook(@RequestBody Map<String, String> map) {
        Integer page = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        String strTagId = map.get("tagId");
        Long tagId;
        if (strTagId == null) {
            tagId = null;
        } else {
            tagId = Long.valueOf(map.get("tagId"));
        }
        String strBookStatus = map.get("bookStatus");
        Byte bookStatus;
        if (strBookStatus == null) {
            bookStatus = null;
        } else {
            bookStatus = Byte.valueOf(map.get("bookStatus"));
        }
        return bookService.getAllBook(page, pageSize, tagId, bookStatus);
    }

    @ResponseBody
    @RequestMapping("/updateBookState")
    public CommonResult updateBookState(@RequestBody Map<String, String> map) {
        Integer state = Integer.valueOf(map.get("state"));
        Long bookId = Long.valueOf(map.get("id"));
        return bookService.updateBookState(state, bookId);
    }

    @ResponseBody
    @GetMapping("/getCurrentBookData")
    public CommonResult getCurrentBookData() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime befor = now.minusDays(7);
        return bookService.getCurrentBookData(befor, now);
    }

    @ResponseBody
    @RequestMapping("/getTotalPage")
    public CommonResult getTotalPage(@RequestBody Map<String,String> map){
        
        return null;
    }
}
